import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

regions = ['华北', '华南', '东北', '西北', '西南', '华东']
sales_2022 = [2354, 1902, 3524, 2698, 2896, 2563]
sales_2021 = [2021, 1563, 3213, 2531, 2631, 2361]
growth_rate = [16, 22, 10, 7, 10, 9]

bar_width = 0.35
index = np.arange(len(regions))

fig, ax1 = plt.subplots(figsize=(10, 6))
bar1 = ax1.bar(index - bar_width/2, sales_2022, bar_width, label='2022年销量', color='#CECCE5')
bar2 = ax1.bar(index + bar_width/2, sales_2021, bar_width, label='2021年销量', color='#C3E2EC')

for i in range(len(sales_2022)):
    ax1.text(i - bar_width/2, sales_2022[i] + 50, str(sales_2022[i]), ha='center', va='bottom', color='black')
    ax1.text(i + bar_width/2, sales_2021[i] + 50, str(sales_2021[i]), ha='center', va='bottom', color='black')

ax2 = ax1.twinx()

line3 = ax2.plot(index, growth_rate, color='#e74c3c', marker='o', linestyle='-', linewidth=2, label='同比增长率')

ax2.set_ylim(0, max(growth_rate) * 1.5)

for i in range(len(growth_rate)):
    ax2.text(i, growth_rate[i] + 0.005, f'{growth_rate[i]}%', ha='center', va='bottom', color='black')

ax1.set_xlabel('区域')
ax1.set_ylabel('销量（单位：件）')
ax2.set_ylabel('同比增长率 (%)')
ax1.set_title('2022 vs 2021 销量对比及同比增长率')
ax1.set_xticks(index)
ax1.set_xticklabels(regions)
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.tight_layout()

#plt.savefig('D:/python charm/01/可视化/第二章/图片/簇状柱形折线图.png')

plt.show()
